<template>
	<view class="President">
		<view class="frame" v-for="(item,index) in patientList" :key="item.id"  @click="handleClick(item)">
			<radio-group @change="selectPatienList">
				<radio class="radio" :value="item.id" v-if="select" />
			</radio-group>
			<view class="frameone">
				<view class="framename">
					姓名：{{item.name}}
				</view>
				<view class="framenumber">
					联系电话：{{item.mobile}}
				</view>
				<view class="framenumber">
					身份证：{{item.id_number}}
				</view>
			</view>
			<view class="framepicture">
				<view class="frametwo" v-if="type!=3" @click="handleClick(item)">
					完善信息
				</view>
			</view>
		</view>
		<!-- 就诊人为空时展示 -->
		<u-empty v-if="patientList.length == 0" mode="list" text="就诊人为空" icon="http://cdn.uviewui.com/uview/empty/list.png">
		</u-empty>
		<view class="A_bottom">
			<view class="">
				<button type="default" v-if="!select" class="button" @click="editpatient">添加就诊人</button>
				<button type="default" v-else class="button" @click="removePatients()">删除就诊人</button>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				//就诊人信息列表
				patientList: [],
				type: 1,
				select: false,
				form: {},
				sele: true,
				// 服务类型的id
				fuwuTypeId: null,
				// 传递过来的service_id
				service_id: "",
				// 分页
				pages: 1,
				// 选中就诊人的id数组
				delectPatientList: []

			}
		},
		onLoad(options) {
			if (options.type) {
				this.type = options.type
			}
			if (this.type == 3) {
				this.form = uni.getStorageSync('./fillin');
			}
			if (options.service_id) {
				this.service_id = options.service_id
			}
		},
		onShow() {
			this.patientList = []
			this.getPatientList()
		},
		methods: {
			handleClick(item) {
				let self = this
				if (this.type == 3) {
					this.form.patient_id = item.id
					this.form.patient_name = item.name
					console.log(this.form);
					uni.setStorageSync('./fillin', this.form)
					uni.navigateBack({
						delta: 1
					})
					return
				} else {
					let PatientInformation = JSON.stringify(item)
					uni.navigateTo({
						url: `./editpatient?item=${PatientInformation}`
					})
				}
			},
			removePatient() {
				this.select = !this.select
				// 清空选中就诊人状态
				this.delectPatientList = [];
			},
			// 删除就诊人功能
			removePatients() {
				this.post('/api/Zrecduzp/shanchuzhidingjihedejiuzhenren', {
					user_id: uni.getStorageSync('user_id'),
					ids: this.delectPatientList
				}).then((res) => {
					this.get('/api/Zrecduzp/jiaozhenxinxiluruhuoqu', {
						user_id: uni.getStorageSync('user_id'),
						id: 0,
						pages: 1
					}).then((res) => {
						this.patientList = res.data

					})
					this.select = !true
					uni.showToast({
						title: '删除成功',
						icon: 'none'
					})
				})
			},
			selectPatienList(e) {
				this.delectPatientList.push(e.detail.value)
			},
			TOPone_j() {
				uni.reLaunch({
					url: "../My"
				})
			},
			editpatient() {
				uni.navigateTo({
					url: "editpatient"
				})
			},
			//获取就诊人信息列表
			getPatientList(index) {
				this.get('/api/Zrecduzp/jiaozhenxinxiluruhuoqu', {
					user_id: uni.getStorageSync('user_id'),
					id: 0,
					page: index ? index : this.pages
				}).then((res) => {
					this.patientList = this.patientList.concat(res.data)

				})
			},
			onReachBottom() {
				this.pages += 1;
				this.getPatientList()
			},
			//计算年龄
			getAge(identityCard) {
				let len = (identityCard + "").length;
				let strBirthday = "";
				if (len == 18) {
					//处理18位的身份证号码从号码中得到生日和性别代码
					strBirthday =
						identityCard.substr(6, 4) +
						"/" +
						identityCard.substr(10, 2) +
						"/" +
						identityCard.substr(12, 2);
				}
				if (len == 15) {
					let birthdayValue = "";
					birthdayValue = identityCard.charAt(6) + identityCard.charAt(7);
					if (parseInt(birthdayValue) < 10) {
						strBirthday =
							"20" +
							identityCard.substr(6, 2) +
							"/" +
							identityCard.substr(8, 2) +
							"/" +
							identityCard.substr(10, 2);
					} else {
						strBirthday =
							"19" +
							identityCard.substr(6, 2) +
							"/" +
							identityCard.substr(8, 2) +
							"/" +
							identityCard.substr(10, 2);
					}
				}
				let birthDate = new Date(strBirthday);
				let nowDateTime = new Date();
				let age = nowDateTime.getFullYear() - birthDate.getFullYear();
				//再考虑月、天的因素;.getMonth()获取的是从0开始的，这里进行比较，不需要加1
				if (
					nowDateTime.getMonth() < birthDate.getMonth() ||
					(nowDateTime.getMonth() == birthDate.getMonth() &&
						nowDateTime.getDate() < birthDate.getDate())
				) {
					age--;
				}
				return age;
			}
		}
	}
</script>

<style>
.radio{
		margin-top: 70rpx;
	}
	.frametub {
		position: absolute;
		right: 40rpx;
		width: 40rpx;
		height: 50rpx;
	}

	.A_bottom {
		width: 100%;
		height: 180rpx;
		margin-bottom: 0;
		background-color: #FFFFFF;
		bottom: 0;
		position: fixed;
		justify-content: center;

	}

	.edit {
		margin-left: 100rpx;
		color: #5F5AF7;
		font-size: 35rpx;
		font-weight: bold;
	}

	.button {
		background-color: #5F5AF7 !important;
		color: #FFFFFF !important;
		width: 600rpx;
		border-radius: 10rpx;
		margin-top: 60rpx;

	}

	.frame {
		position: relative;
	}
	/* 信息栏 */
	.frameage {
		width: 25rpx;
		height: 25rpx;
		margin-top: 70rpx;
	}

	.frametwo {
		color: #5F5AF7;
		height: 180rpx;
		line-height: 180rpx;
		min-width: 100rpx;
		position: absolute;
		right: 40rpx;
		top: -30rpx;
		text-align: right;
	}

	.framepicture {
		display: flex;

	}

	.frameone {
		margin-left: 50rpx;
	}

	.framenumber {
		height: 50rpx;
		color: #666666;
	}

	.framename {
		font-weight: 900;
		height: 80rpx;
		line-height: 80rpx;
	}

	.frametu {
		width: 40rpx;
		height: 40rpx;
		position: absolute;
		right: 40rpx;

	}

	.left_T {
		width: 50rpx;
		height: 50rpx;
		margin-top: 50rpx;
	}

	.frame {
		margin: 50rpx;
		width: 680rpx;
		background-color: #FFFFFF;
		margin-left: 50rpx;
		display: flex;
		border-radius: 40rpx;

	}

	/* 顶部样式 */
	.Top {
		display: flex;
		padding: 20rpx;
	}

	.Topone {
		width: 280rpx;
		text-align: left;
		font-size: 45rpx;
		font-weight: 900;
		margin-left: 50rpx;

	}

	.Toptwo {
		color: #5F5AF7;
		font-size: 35rpx;
		font-weight: 900;
		margin-left: -50rpx;
		margin-top: rpx;
	}

	.President {
		background-color: #f9f9f9;
		padding-bottom: 200rpx;
	}
</style>
<style>
	page{
		background-color: #f9f9f9;
	}
</style>
